package com.Ugent.Minerva;

import java.io.Serializable;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class login extends Activity implements OnClickListener {
	public static final String PREFS ="Prefs";
	static SharedPreferences prefs;
	SharedPreferences.Editor editor;

	Button inlog;
	EditText username;
	EditText password;
	Student s1 = new Student();
	ArrayList<Course> courseList = new ArrayList<Course>();
	public static final int LOGIN = 0;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//only portrait mode
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		prefs = getSharedPreferences(PREFS, 0);
		editor = prefs.edit();
		
		
		// login-button
		this.inlog = (Button) this.findViewById(R.id.btn_sign_in);
		this.inlog.setOnClickListener(this);
		username = (EditText) findViewById(R.id.txt_username);
		password = (EditText) findViewById(R.id.txt_password);
		// check if user and pass have already been stored
		String user = prefs.getString("username", "");
		if ("".equals(user)){	
		}
		else {
			username.setText(user);
		}
		String pass =prefs.getString("password", "");
		if ("".equals(pass)){
		}
		else {
			password.setText(pass);
		}
		
	}

	@Override
	// action of button
    public void onClick(View v) {
				
	    //use AsyncTask to show dialogue
		new ProgressTask().execute();

	}

	public void clear (boolean i){
		
	}
	
	public class ProgressTask extends AsyncTask<Void, Void, Void> {
	    ProgressDialog dialog;
	  
	    @Override
		protected void onPreExecute() {
			super.onPreExecute();
			showDialog(LOGIN);
		}

	    @Override
	    protected Void doInBackground(Void... params) {
			String sUserName = "";
			String sPassword = "";
			// look if username is stored
				
			String user = prefs.getString("username", "");
			if ("".equals(user)){
				editor.putString("username", username.getText().toString());
				editor.commit();
				sUserName = username.getText().toString();
					
			}
			else {sUserName = username.getText().toString();}
				
			// look if password is stored				
			String pass =prefs.getString("password", "");
			if ("".equals(pass)){
				editor.putString("password", password.getText().toString());
				editor.commit();
				sPassword = password.getText().toString();
			}
			else {sPassword = password.getText().toString();}

			//login
			Object result = Soap.login(sUserName, sPassword);
			String ticket= result.toString();
			//set ticket to Student    
			s1.setTicket(ticket);
			
			if (ticket.equals("error")){
				//if error in login go to error page
				Intent intent = new Intent(getApplicationContext(), LoginError.class);
	            intent.putExtra("LoginMessage", "Unable to login");
	            startActivity(intent);
			}
			else {
				//identify user + naam doorgeven
				String User = Soap.identifyUser(s1.getTicket()).toString();
				System.out.println(User);
				//naam
		        int z = User.indexOf(",");
		        int x = User.indexOf(",",z+1);
		        String voornaam = User.substring(1,z);
		        String achternaam = User.substring(z+1,x);
		        String naam = voornaam+achternaam;
		        s1.setName(naam);

	            //student code
		        int y = User.indexOf(",",x+1);
		        String officialCode = User.substring(y+2,(User.length()-1));
		        s1.setofficialCode(officialCode);
		          
		        //get course with studentobject
		        courseList = Facade.getCourses(s1.getOfficialCode());    
				Intent intent = new Intent(getApplicationContext(), CourseList.class);
				intent.putExtra("courseList", (Serializable) courseList); 
				intent.putExtra("student", (Serializable) s1); 
	            startActivity(intent);
				}
				
				return null;
			}
	        
	        @Override
			protected void onPostExecute(Void unused) {
				dismissDialog(LOGIN);
			}
	    }
	
	//create dialog progress
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
			case LOGIN:
				ProgressDialog mProgressDialog;
				mProgressDialog = new ProgressDialog(this);
				mProgressDialog.setMessage("Logging in...");
				mProgressDialog.setCancelable(false);
				mProgressDialog.show();
				return mProgressDialog;
			default:
				return null;
		}
	}
}
	
